package com.masiyi.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

/**
 * @Author masiyi
 * @Date 2023/12/18
 * @PackageName:com.masiyi.servlet
 * @ClassName: MySession
 * @Description: TODO
 * @Version 1.0
 */
@WebServlet(urlPatterns = "/mySession3")
public class MySession3 extends HttpServlet {

    /**
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(req.getRequestId());
        HttpSession session = req.getSession();
        session.setAttribute("name", "masiyi3");
        //失效
//        session.setMaxInactiveInterval(3);
        //失效
//        session.invalidate();
        String uri = resp.encodeURL("/mySession4");
        resp.sendRedirect(uri);
        System.out.println(uri);
        System.out.println("setAttribute");
        System.out.println(session.getId());
    }
}
